/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SalasananVaihto.java
 *
 * Created on 20.2.2009, 9:22:57
 */
package kayttoliittyma;

import java.awt.Color;
import opintopaivakirja.*;

/**
 *
 * @author Erkkinuuttinen
 */
public class SalasananVaihto extends javax.swing.JDialog {

    /** Creates new form SalasananVaihto
     * @param parent
     * @param modal 
     * @param kayttaja
     */
//    public SalasananVaihto(java.awt.Frame parent, boolean modal) {
//        super(parent, modal);
//        initComponents();
//    }
    public SalasananVaihto(javax.swing.JDialog parent, boolean modal, Kayttaja kayttaja) {
        super(parent, modal);
        initComponents();

        this.kayttaja = kayttaja;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPasswordFieldUusi2 = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        jPasswordFieldVanha = new javax.swing.JPasswordField();
        jPasswordFieldUusi1 = new javax.swing.JPasswordField();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jButtonTallenna = new javax.swing.JButton();
        jButtonPeruuta = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Uusi salasana:");
        jLabel1.setName("jLabel1"); // NOI18N

        jPasswordFieldUusi2.setName("jPasswordFieldUusi2"); // NOI18N

        jLabel2.setText("Vanha salasana:");
        jLabel2.setName("jLabel2"); // NOI18N

        jPasswordFieldVanha.setName("jPasswordFieldVanha"); // NOI18N

        jPasswordFieldUusi1.setName("jPasswordFieldUusi1"); // NOI18N

        jLabel12.setForeground(new java.awt.Color(255, 0, 0));
        jLabel12.setText("*");
        jLabel12.setName("jLabel12"); // NOI18N

        jLabel13.setForeground(new java.awt.Color(255, 0, 0));
        jLabel13.setText("*");
        jLabel13.setName("jLabel13"); // NOI18N

        jLabel14.setForeground(new java.awt.Color(255, 0, 0));
        jLabel14.setText("*");
        jLabel14.setName("jLabel14"); // NOI18N

        jButtonTallenna.setText("Tallenna");
        jButtonTallenna.setName("jButtonTallenna"); // NOI18N
        jButtonTallenna.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonTallennaActionPerformed(evt);
            }
        });

        jButtonPeruuta.setText("Peruuta");
        jButtonPeruuta.setName("jButtonPeruuta"); // NOI18N
        jButtonPeruuta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonPeruutaActionPerformed(evt);
            }
        });

        jLabel3.setText("Uusi salasana:");
        jLabel3.setName("jLabel3"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPasswordFieldVanha, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPasswordFieldUusi1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPasswordFieldUusi2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jLabel14, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(62, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(116, Short.MAX_VALUE)
                .addComponent(jButtonPeruuta)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonTallenna)
                .addGap(43, 43, 43))
            .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPasswordFieldVanha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPasswordFieldUusi1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jPasswordFieldUusi2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonPeruuta)
                    .addComponent(jButtonTallenna))
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonPeruutaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPeruutaActionPerformed
        this.setVisible(false);
    // TODO add your handling code here:
    }//GEN-LAST:event_jButtonPeruutaActionPerformed

    private void jButtonTallennaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonTallennaActionPerformed
        //Kayttaja kayttaja = new Kayttaja();

        String salasanaVanha = jPasswordFieldVanha.getText().trim();
        String salasana1 = jPasswordFieldUusi1.getText().trim();
        String salasana2 = jPasswordFieldUusi2.getText().trim();

        if (!salasana1.equals(salasana2) || !salasanaVanha.equals(kayttaja.getSalasana())) {
            if (!salasana1.equals(salasana2)) {
                //jLabelOhjeSalasana.setForeground(Color.red);
                jPasswordFieldUusi1.setBackground(Color.pink);
                jPasswordFieldUusi2.setBackground(Color.pink);
            }
            if (!salasanaVanha.equals(kayttaja.getSalasana())) {
                jPasswordFieldVanha.setBackground(Color.pink);
            }
        } else {
            kayttaja.setSalasana(salasana1);
            //kayttaja.updateKayttaja();
            kayttaja.tallennaKayttaja();
            this.setVisible(false);
            
        }
    //kayttaja.getSalasana()

    //this.setVisible(false);
    // TODO add your handling code here:
    }//GEN-LAST:event_jButtonTallennaActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                SalasananVaihto dialog = new SalasananVaihto(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonPeruuta;
    private javax.swing.JButton jButtonTallenna;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField jPasswordFieldUusi1;
    private javax.swing.JPasswordField jPasswordFieldUusi2;
    private javax.swing.JPasswordField jPasswordFieldVanha;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables
    Kayttaja kayttaja;
}
